package com.jcfk.eam.controller.parent;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.*;

import com.jcfk.eam.domain.dto.main.EamRepayRecordDTO;
import com.jcfk.eam.domain.query.main.EamAssetInfoQuery;
import com.jcfk.eam.domain.query.main.EamRepayRecordQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.jcfk.eam.domain.query.main.EamGrantRecordQuery;
import com.jcfk.eam.domain.dto.main.EamGrantRecordDTO;
import com.jcfk.eam.domain.dto.main.EamGrantRecordDetailDTO;
import com.jcfk.eam.service.main.EamGrantRecordService;

import java.util.List;

/**
 * 发放记录
 *
 * @author yql
 * @email 522891237@gqq.com
 * @date 2024-07-29
 */
@Api(tags = {"发放记录管理"})
@RestController
@RequestMapping("eamgrantrecord")
public class EamGrantRecordController {
    @Autowired
    private EamGrantRecordService eamGrantRecordService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamGrantRecordQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询发放记录")
    @PostMapping("/queryByPage")
    public RetPage<EamGrantRecordDTO> queryByPage(@RequestBody ReqPageParam<EamGrantRecordQuery> requestParam) {
        //设置默认排序
        if(requestParam.getSort().getOrderItem()==null){
            ReqSortParam<EamGrantRecordQuery> sortParam=new ReqSortParam();
            sortParam.setColumn("grantDate");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }

        DataPage<EamGrantRecordDTO> page = new DataPage<EamGrantRecordDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                EamGrantRecordDTO.class);
        page = eamGrantRecordService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }

    /**
     * 分页归还列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamGrantRecordDetailQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询归还记录明细")
    @PostMapping("/queryRepayByPage")
    public RetPage<EamRepayRecordDTO> queryRepayByPage(@RequestBody ReqPageParam<EamRepayRecordQuery> requestParam) {
        DataPage<EamRepayRecordDTO> page = new DataPage<>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                EamRepayRecordDTO.class);
        requestParam.getData().setIsAudit(1);
        page = eamGrantRecordService.queryRepayByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }

    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamGrantRecordQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取发放记录列表", notes = "获取发放记录列表", httpMethod = "POST")
    @PostMapping("getDetails")
    public RetResult<List<EamGrantRecordDetailDTO>> getDetails(@RequestBody EamGrantRecordQuery query) {

        List<EamGrantRecordDetailDTO> list = eamGrantRecordService.getDetails(query);

        return RetResult.ok(list);
    }

    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamGrantRecordQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取发放记录列表", notes = "获取发放记录列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<List<EamGrantRecordDTO>> getList(@RequestBody EamGrantRecordQuery query) {

        List<EamGrantRecordDTO> list = eamGrantRecordService.getList(query);

        return RetResult.ok(list);
    }


    /**
     * 发放记录
     * 根据id 查询详情
     */
    @ApiOperation("查询发放记录")
    @GetMapping("/get/{id}")
    public RetResult<EamGrantRecordDTO> get(@PathVariable("id") String id) {
        EamGrantRecordDTO item = eamGrantRecordService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 归还记录
     * 根据id 查询详情
     */
    @ApiOperation("查询发放记录")
    @GetMapping("/getDetails/{id}")
    public RetResult<EamRepayRecordDTO> getDetails(@PathVariable("id") String id) {
        EamRepayRecordDTO item = eamGrantRecordService.getDetails(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理发放记录
     */
    @ApiOperation("保存发放记录")
    @PostMapping("/save")
    @RequestLogHeader(modular = "发放记录管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamGrantRecordDTO eamGrantRecord) {
        // 数据校验
        RetResult ret = eamGrantRecordService.isValid(eamGrantRecord);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(eamGrantRecord.getId())) {
            // 新增
            eamGrantRecordService.insert(eamGrantRecord);
        } else {
            // 更新
            eamGrantRecordService.update(eamGrantRecord);
        }
        return RetResult.ok();

    }


    /**
     * 批量删除发放记录
     */
    @ApiOperation("批量删除发放记录")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "发放记录管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamGrantRecordDTO eamGrantRecord) {
        return RetResult.ok(eamGrantRecordService.logicDeleteList(eamGrantRecord));
    }

    /**
     * 批量审核发放记录
     */
    @ApiOperation("批量审核发放记录")
    @PostMapping("/examine")
    @RequestLogHeader(modular = "发放记录管理", businessType = "批量审核")
    public RetResult<?> examine(@RequestBody EamGrantRecordDTO eamGrantRecord) {
        return RetResult.ok(eamGrantRecordService.logicExamineList(eamGrantRecord));
    }



    /**
     * 删除发放记录
     */
    @ApiOperation("删除发放记录")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "发放记录管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamGrantRecordService.deleteitem(id);
        return RetResult.ok();
    }
}
